package Ch02.JianDa;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class JianDa1 {
    public static void main(String[] args) throws Exception {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        //解析XML文档，得到一个Document对象，即DOM树
        Document doc=db.parse("src/Ch02/JianDa/Studeng.xml");

//        NodeList studentList = doc.getElementsByTagName("student");
//        for (int i = 0; i < studentList.getLength(); i++) {
//            Node studentElement = studentList.item(i);
//            Element element = (Element) studentElement;
//            String value=element.getAttribute("name");
//            NodeList types=element.getChildNodes();
//            for (int j = 0; j < types.getLength(); j++) {
//                Element typeElement=((Element) types.item(j));
//                String type=typeElement.getAttribute("name");
//                System.out.println("手机:"+value+type);
//            }
//        }
//        NodeList list1 = doc.getElementsByTagName("name");
//        Element nameElement=(Element) list1.item(1);
//        String name=nameElement.getFirstChild().getNodeValue();
//
//        NodeList list2 = doc.getElementsByTagName("course");
//        Element courseElement=(Element) list2.item(1);
//        String course=courseElement.getFirstChild().getNodeValue();

        //读取scores
        NodeList list= doc.getElementsByTagName("scores");
        //scores元素节点
        Element scoresElement=(Element) list.item(1);
        //读取文本节点
        String scores=scoresElement.getFirstChild().getNodeValue();

        System.out.println("成绩为:"+scores);
    }
}
